﻿using System;
using System.Windows.Forms;
using ContractManager.Common.Class;
using ContractManager.Common.FormBase;
using ContractManager.Entity;
using DevExpress.XtraEditors;
using ContractManager.BLL.Business;

namespace ContractManager.Lists.Forms
{
    public partial class FrmEmployeeDetail : XtraFormBase
    {
        
        #region "public properties"
        public int EmployeeId { get; set; }
        #endregion

        #region "Method Construction"
        public FrmEmployeeDetail()
        {
            InitializeComponent();
            
        }
        #endregion

        #region "private Method"

        private void GetDepartment()
        {
            txtDepartmentId.Properties.DataSource = ClsDepartments.SelectAll();
            txtDepartmentId.Properties.DisplayMember = "Name";
            txtDepartmentId.Properties.ValueMember = "ID";

            txtMbID.Properties.DataSource = ClsMemberBranch.SelectAll();
            txtMbID.Properties.DisplayMember = "MbName";
            txtMbID.Properties.ValueMember = "MbID";
        }

        private void BlankForm()
        {
            
            this.ResetControls();
        }

        private void GetDetail()
        {

            var detail = ClsEmployees.SelectByPk(EmployeeId);
            if (detail != null)
            {
                txtDepartmentId.EditValue = detail.DepartmentId;
                txtUserName.EditValue = String.Format("{0}", detail.UserName);
                txtFullName.EditValue = String.Format("{0}", detail.FullName);
                txtPassword.EditValue = ClsVariable.Decrypt(String.Format("{0}", detail.Password));
                txtPosition.EditValue = String.Format("{0}", detail.Position);
                txtAddress.EditValue = String.Format("{0}", detail.Address);
                txtPhoneNumber.EditValue = String.Format("{0}", detail.PhoneNumber);
                txtPhoneMobile.EditValue = String.Format("{0}",detail.PhoneMobile);
                txtDescription.EditValue = String.Format("{0}",detail.Description);
                txtBusinessRole.SelectedIndex = detail.BusinessRole - 1;
                chkIsEditVoucher.EditValue = detail.IsEditVoucher;

                txtMbID.EditValue = detail.MbID;
            }
        }

        private void DoSave()
        {
            #region "Check Condition"
            if (txtDepartmentId.EditValue == null)
            {
                XtraMessageBox.Show(
                                "Phòng ban không được để trống", 
                                "Thông báo", 
                                MessageBoxButtons.OK, 
                                MessageBoxIcon.Error);
                txtDepartmentId.SelectAll();
                txtDepartmentId.Focus();
                return;
            }

            if (txtUserName.EditValue == null)
            {
                XtraMessageBox.Show(
                                "Mã nhân viên không được để trống",
                                "Thông báo",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                txtUserName.SelectAll();
                txtUserName.Focus();
                return;
            }

            if (txtBusinessRole.EditValue == null)
            {
                XtraMessageBox.Show(
                                "Quyền hạn nhân viên không được để trống",
                                "Thông báo",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                txtBusinessRole.SelectAll();
                txtBusinessRole.Focus();
                return;
            }

            #endregion

            Employees item = EmployeeId <= 0 ? new Employees() : ClsEmployees.SelectByPk(EmployeeId);

            item.ID = EmployeeId;
            item.DepartmentId = Convert.ToInt32(txtDepartmentId.EditValue);
            item.UserName = String.Format("{0}", txtUserName.EditValue);
            item.FullName = String.Format("{0}", txtFullName.EditValue);
            item.Password = ClsVariable.Encrypt(String.Format("{0}", txtPassword.EditValue));
            item.Position = String.Format("{0}", txtPosition.EditValue);
            item.Address = String.Format("{0}", txtAddress.EditValue);
            item.PhoneNumber = String.Format("{0}", txtPhoneNumber.EditValue);
            item.PhoneMobile = String.Format("{0}", txtPhoneMobile.EditValue);
            item.Description = String.Format("{0}", txtDescription.EditValue);
            item.BusinessRole = txtBusinessRole.SelectedIndex + 1;
            item.IsEditVoucher = chkIsEditVoucher.Checked;

            if (txtMbID.EditValue != null)
            {
                item.MbID = Convert.ToInt32(txtMbID.EditValue);
            }
            
            try
            {
                if (EmployeeId > 0)
                {
                    ClsEmployees.Update(item);
                }
                else
                {
                    var citem = ClsEmployees.SelectByUserName(item.UserName);
                    if (citem != null)
                    {
                        XtraMessageBox.Show(
                            "Mã người dùng không được phép trùng.",
                            "Thông báo",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                        txtUserName.SelectAll();
                        txtUserName.Focus();
                        return;
                    }
                    ClsEmployees.Insert(item);
                    EmployeeId = item.ID;
                }
                Close();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(
                        "Có lỗi xảy ra trong quá trình cập nhật dữ liệu. " + ex.Message, 
                        "Thông báo", 
                        MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
            }
        }

        #endregion

        #region "Event Form Load"
        private void FrmEmployeeDetailLoad(object sender, EventArgs e)
        {
            BlankForm();
            GetDepartment();
            txtUserName.Properties.ReadOnly = EmployeeId > 0;
            if (EmployeeId > 0) GetDetail();
            if (!String.IsNullOrEmpty(FormXtraLayoutTemplate))
            {
                var fileLayout = ClsVariable.GetRestoreLayoutFullPath(FormXtraLayoutTemplate);
                layoutControl1.RestoreLayoutFromXml(fileLayout);
            }
        }
        #endregion

        #region "Button Event"
        private void BtnSaveClick(object sender, EventArgs e)
        {
            DoSave();
        }

        private void BtnExitClick(object sender, EventArgs e)
        {
            Close();
        }
        #endregion
    }
}